import 'package:dio/dio.dart';
import 'package:flutter/material.dart';

class HTTP {
  Future pushmsg(String uid, String topic, String str) async {
    String api = "https://apis.bemfa.com/va/postJsonMsg";
    var dio = Dio(BaseOptions(headers: {
      "Content-Type": "application/json; charset=utf-8",
      "UserAgent":
          "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36 Edge/18.17763",
    }));

    Response res = await dio
        .post(api, data: {"uid": uid, "topic": topic, "type": 3, "msg": str});
    // code状态码	说明
    // 0	成功
    // 10002	请求参数有误
    // 40000	未知错误
    // 40004	私钥或主题错误
    debugPrint(res.data['code'].toString());
  }

  Future getmsg(String uid, String topic) async {
    String api = "https://apis.bemfa.com/va/getmsg";
    var dio = Dio(BaseOptions(headers: {
      "Content-Type": "application/json; charset=utf-8",
      "UserAgent":
          "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36 Edge/18.17763"
    }));

    Response res = await dio
        .get(api, queryParameters: {"uid": uid, "topic": topic, "type": 3});
    int resCode = res.data['code']!;
    List msgData = res.data['data']!;
    if (resCode == 10002) {
      debugPrint(res.data['message'].toString());
      return res.data['message'];
    }
    if (resCode == 0) {
      return msgData[0];
      // String msgTime = msgData[0]['time'];
      // double prices = double.parse(msgData[0]['msg']);
      // updatePricesLogic.nowPrices.value = prices;
      // updatePricesLogic.pricesUpdateTime.value = msgTime;
    }
  }
}
